package com.lanou.moving.activity;

/**
 * Created by dllo on 15/10/23.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.lanou.moving.R;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.entity.MusicThemeRoot;

/**
 * Created by dllo on 15/9/18.
 */

public class WebViewActivity extends BaseActivity {
    private WebView webView;
    private String link;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        BaseApplication.getInstance().addActivity(this);
        webView = (WebView) findViewById(R.id.webView);
        Intent intent = getIntent();
        MusicThemeRoot.DataEntity music_theme = (MusicThemeRoot.DataEntity) intent.getSerializableExtra("music_theme");
        if (music_theme != null){
            link = music_theme.getActionUrl();
        }else {
            link = intent.getStringExtra("link");
        }

        // 从webView中获取WebSettings设置对象
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDisplayZoomControls(true);
        settings.setDefaultFixedFontSize(15);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                WebViewActivity.this.setProgress(newProgress * 1000);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Toast.makeText(WebViewActivity.this, "网页加载失败", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置要加载的网页地址
        webView.loadUrl(link);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;

        }

        return super.onKeyDown(keyCode,event);
    }
}

